package com.zzx.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonMerge;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails {
    private Manager manager;
    private List<String> permissionList;
    @JSONField(serialize = false)
    private Set<GrantedAuthority> permissionList2;

    public LoginUser(Manager manager,List<String> permissionList){
        this.manager = manager;
        this.permissionList = permissionList;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if(permissionList==null)  return null;
        if(permissionList2!=null)  return permissionList2;
            //赋予权限
        permissionList2 =  new HashSet<>();
        for (String s : permissionList) {
            permissionList2.add(new SimpleGrantedAuthority(s));
        }
        return permissionList2;
    }

    @Override
    public String getPassword() {
        return manager.getMgPwd();
    }

    @Override
    public String getUsername() {
        return manager.getMgName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
